<?php

namespace StudyBuddy;

/**
 * This class renders extra "Check boxes" on
 * the "Ask" and "Answer" forms
 * To ask user to also post to Twitter, Facebook, etc..
 *
 */
class SocialCheckboxes {

    /**
     * Make html with divs containint checkboxes
     * and labels for the extra "post to Twitter" etc.
     * checkboxes
     * a bunch of divs with checkboxes to
     * post content to Twitter, Facebook etc., depending
     * on settings in !config.ini
     * and depending if these extra modules are enabled
     *
     * @return string html fragment
     */
    public static function get(Registry $oRegistry) {

        d('cp');
        $oViewer = $oRegistry->Viewer;
        if ($oViewer->isGuest()) {
            d('User is guest, no social checkboxes to be added for guest');
            return '';
        }

        $ret = '';
        $aFilters = $oRegistry->Ini->getSection('INPUT_FILTERS');
        d('$aFilters: ' . print_r($aFilters, 1));
        /**
         * @todo Translate String
         */
        $tpl = 'Post to %s<br><strong>+%s</strong> reputation points';

        /**
         * If has twitter observer module
         */
        if (array_key_exists('twitter', $aFilters)) {
            d('cp');
            /**
             * The state of checkbox remembered from
             * the previous user action + must not have Twitter access revoked
             * Enter description here ...
             * @var unknown_type
             */
            $isConnected = ('' !== (string) $oViewer->getTwitterSecret());
            $checked = ( $isConnected && (true === $oViewer['b_tw'])) ? ' checked' : '';
            d('$checked: ' . $checked);
            $label = sprintf($tpl, 'Twitter', \StudyBuddy\Points::SHARED_CONTENT);
            $vars = array('tweet', $label, $checked);
            $ret .= \tplSocialPost::parse($vars, false);
        }

        /**
         * Is has facebook observer module
         */
        if (array_key_exists('facebook', $aFilters)) {
            $isFbConnected = (1 < \strlen((string) $oViewer->getFacebookToken()));
            $checked = ($isFbConnected && true === $oViewer['b_fb']) ? ' checked' : '';
            $label = sprintf($tpl, 'Facebook', \StudyBuddy\Points::SHARED_CONTENT);
            $vars = array('facebook', $label, $checked);
            $ret .= \tplSocialPost::parse($vars, false);
        }

        /**
         * Is has tumblr observer module
         */
        if (array_key_exists('tumblr', $aFilters)) {
            $isTmConnected = (null !== $oViewer->getTumblrToken());
            $checked = ($isTmConnected && true === $oViewer['b_tm']) ? ' checked' : '';
            $label = sprintf($tpl, 'Tumblr', \StudyBuddy\Points::SHARED_CONTENT);
            $vars = array('tumblr', $label, $checked);
            $ret .= \tplSocialPost::parse($vars, false);
        }

        /**
         * Is has blogger observer module
         */
        if (array_key_exists('blogger', $aFilters)) {
            $isBConnected = (null !== $oViewer->getBloggerToken());
            $checked = ($isBConnected && true === $oViewer['b_bg']) ? ' checked' : '';
            $label = sprintf($tpl, 'Blogger', \StudyBuddy\Points::SHARED_CONTENT);
            $vars = array('blogger', $label, $checked);
            $ret .= \tplSocialPost::parse($vars, false);
        }


        /**
         * Is has LinkedIn observer module
         */
        //if(true || array_key_exists('linkedin', $aFilters)){
        if (array_key_exists('linkedin', $aFilters)) {
            $isLConnected = (null !== $oViewer->getLinkedInToken());
            $checked = ($isLConnected && true === $oViewer['b_li']) ? ' checked' : '';
            $label = sprintf($tpl, 'LinkedIn', \StudyBuddy\Points::SHARED_CONTENT);
            $vars = array('linkedin', $label, $checked);
            $ret .= \tplSocialPost::parse($vars, false);
        }


        d('ret: ' . $ret);

        return $ret;
    }

}
